﻿using HomagChina.Basic.CCT.Tools;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;

namespace Proxy
{
    public class DealWithFiles
    {
        public DealWithFiles()
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="Source"></param>
        /// <param name="Separator"></param>
        /// <param name="Format"></param>
        /// <returns></returns>
        public List<BoardInfoConstruct> DealWithBoardInfoFiles(string Source, char Separator, string Format)
        {
            CCHSAnalyzer cCHSAnalyzer = new CCHSAnalyzer();
            List<BoardInfoConstruct> list = new List<BoardInfoConstruct>();
            try
            {
                list = cCHSAnalyzer.BoardsInfoAnalyzer(Source, Separator, Format);

                return list;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_DealWithCsv.Error(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 单层级文件夹
        /// </summary>
        /// <param name="Source"></param>
        public void DealWithBoardInfoFilesByAuto()
        {
            var Source = ConfigurationManager.AppSettings["CsvPath"];
            var HistoryPath = ConfigurationManager.AppSettings["CsvHistoryPath"];
            FolderAnalyzer folderAnalyzer = new FolderAnalyzer();
            Mes2CCTCsvAnalyzer mes2CCTCsvAnalyzer = new Mes2CCTCsvAnalyzer();
            do
            {
            Start:
                Thread.Sleep(1000 * 60);
                try
                {
                    if (Globle.isInfeedActive)
                    {
                        string[] files = Directory.GetFiles(Source);
                        //存在待处理的批次文件
                        if (files.Length > 0)
                        {
                            var csvName = files.FirstOrDefault(s => s.EndsWith(".csv"));
                            if (csvName == null)
                            {
                                goto Start;
                            }
                            HistoryPath = HistoryPath + csvName.Split('\\').Last().ToString();
                            if (mes2CCTCsvAnalyzer.DealWithMes2CCTCsvFile(csvName, ','))
                            {
                                if (File.Exists(HistoryPath))
                                {
                                    File.Delete(HistoryPath);
                                }
                                File.Move(csvName, HistoryPath);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_DealWithCsv.Error(ex.Message);
                }
            } while (true);
        }

        /// <summary>
        /// 单文件选择
        /// </summary>
        /// <param name="Source"></param>
        /// <param name="Separator"></param>
        /// <param name="Format"></param>
        /// <returns></returns>
        public StackInfoConstruct DealWithJobStackInfoFiles(string Source, char Separator, string Format)
        {
            try
            {
                VirtualStorageJob2CCHSAnalyzer virtualStorageJob2CCHSAnalyzer = new VirtualStorageJob2CCHSAnalyzer();
                var info = virtualStorageJob2CCHSAnalyzer.JobStackInfoAnalyzer(Source, Separator, Format);
                return info;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
        }
    }
}